package com.ruoyi.project.newhope.transfer.util;

import lombok.extern.slf4j.Slf4j;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class CommonInvoke {

    public CommonInvoke() {
    }

    public String invokeWS(String IFACE_CODE, String REQUEST_DATA, String ESB_SN) throws Exception {

        String urlStr = OracleEBSUtil.getAddress() + "/ebsIntergration/commonWS";
        URL wsUrl = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        conn.setReadTimeout(600000);
        conn.setConnectTimeout(600000);
        OutputStream os = conn.getOutputStream();

        String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
                + "                  xmlns:esb=\"http://w3.ibm.com/gbs/ais/ei/esb\">\n" + "  <soapenv:Header/>\n"
                + "  <soapenv:Body>\n" + "    <esb:REQUEST>\n"
                + "      <esb:ESB_ATTRS>\n" + "        <esb:ESB_SID>FXiaoKe</esb:ESB_SID>\n" + "        <esb:ESB_USER>FXiaoKe</esb:ESB_USER>\n"
                + "        <esb:ESB_PWD>f89b237fe309aac9c361162e001850a6" +
                "</esb:ESB_PWD>\n" + "        <esb:ESB_SN>" + ESB_SN
                + "</esb:ESB_SN>\n" + "        <esb:IFACE_CODE>" + IFACE_CODE + "</esb:IFACE_CODE>\n"
                + "        <esb:ESB_SERVICE_ID>A0020000000007</esb:ESB_SERVICE_ID>" + "<esb:ARG_1/>"
                + "<esb:ARG_2/>" + "<esb:ARG_3/>" + "<esb:ARG_4/>" + "<esb:ARG_5/>\n"
                + "      </esb:ESB_ATTRS>\n" + "      <esb:REQUEST_DATA>" + REQUEST_DATA + "</esb:REQUEST_DATA>\n"
                + "    </esb:REQUEST>\n" + "  </soapenv:Body>\n" + "</soapenv:Envelope>\n";
        ;

        log.info("SOAP: {}", soap);
        os.write(soap.getBytes());
        InputStream is = conn.getInputStream();

        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while ((len = is.read(b)) != -1) {
            String ss = new String(b, 0, len, "UTF-8");
            s += ss;
        }
        is.close();
        os.close();
        conn.disconnect();
        return s;
    }

    public String createRequestData(List input, String IFACE_CODE, String ESB_SN, int lineCount) {
        String s = "<inv:InputParameters xmlns:inv=\"http://xmlns.oracle.com/apps/cux/soaprovider/plsql/cux_0_ws_server_prg/invokefmsws/\">\n"
                + "  <inv:P_IFACE_CODE>" + IFACE_CODE + "</inv:P_IFACE_CODE>\n" + "  <inv:P_BATCH_NUMBER>" + ESB_SN
                + "</inv:P_BATCH_NUMBER>\n" + "  <inv:P_LINE_COUNT>" + lineCount + "</inv:P_LINE_COUNT>\n"
                + "  <inv:P_SOURCE_CODE>T001_SUPP</inv:P_SOURCE_CODE>\n" + "  <inv:P_REQUEST_LINE>\n";

        for (int i = 0; i < input.size(); i++) {

            String tep = input.get(i).toString();

            s = s + tep;

        }
        if (input.size() == 0) {
            s = s + "<inv:P_REQUEST_LINE_ITEM>\n" + "</inv:P_REQUEST_LINE_ITEM>\n";
        }
        s = s + "  </inv:P_REQUEST_LINE>\n" + "</inv:InputParameters>";
        String encode = Base64Util.encodeStr(s);
        return encode;
    }

    public String createSN(String IFACE_CODE) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String str = sdf.format(date);
        String ESB_SN = "YZ01" + str;
        log.info("----------------sn----------------------:{}", ESB_SN);
        return ESB_SN;
    }

    public HashMap<String, String> getNodes(Element node, HashMap<String, String> map) {
        map.put(node.getName(), node.getTextTrim());
        List<Attribute> listAttr = node.attributes();
        for (Attribute attr : listAttr) {
            String name = attr.getName();
            String value = attr.getValue();
        }

        List<Element> listElement = node.elements();
        for (Element e : listElement) {
            map = this.getNodes(e, map);
        }
        return map;
    }

}
